package com.unicom.office.func;

import com.unicom.office.dataitem.ObjectDataItem;
import com.unicom.office.matcher.DataMatcher;
import com.unicom.office.runtime.OfficeRuntime;
import com.unicom.office.type.ObjectTypeItem;
import com.unicom.office.type.TypeItem;
import com.unicom.office.unit.OfficeUnit;
import com.unicom.office.var.VarItem;

public class ObjectPropFunc extends FuncBase {
    public FuncBase objectFunc;
    public VarItem key;

    public void setObjectFunc(FuncBase objectFunc) {
        if(!(objectFunc.getType() instanceof ObjectTypeItem)){
            throw new RuntimeException("类型不匹配");
        }
        this.objectFunc = objectFunc;
    }

    public void setKey(VarItem key) {
        this.key = key;
    }

    public Object run(OfficeRuntime officeRuntime) {
        ObjectDataItem map = (ObjectDataItem) objectFunc.run(officeRuntime);
        return map.getProp(key.getName());
    }

    public void runForMatch(OfficeUnit unit, OfficeRuntime officeRuntime) {
        ObjectDataItem map = (ObjectDataItem) objectFunc.run(officeRuntime);
        DataMatcher dataMatcher = new DataMatcher(null, unit);
        Object match = dataMatcher.match();
        map.setProp(key.getName(), match);
    }

    public TypeItem getType() {
        return key.getTypeItem();
    }
}
